<template>
    <Modal v-model="visible" :title="title" @on-ok="ok" :mask-closable="false">
        <Form ref="form" :model="form" :label-width="100" onsubmit="return false">

            <FormItem label="姓名" prop="name">
                <Input v-model="form.name"/>
            </FormItem>

            <FormItem label="手机号" prop="telephone">
                <Input v-model="form.telephone"/>
            </FormItem>

        </Form>

        <div slot="footer">
            <Button type="text" size="large" @click.native="cancel">取消</Button>
            <Button type="primary" size="large" :loading="loading" @click.native="ok">确定</Button>
        </div>
    </Modal>
</template>

<script>
    import api from '../../api';

    export default {
        props: ['getData'],
        data () {
            return {
                visible: false,
                loading: false,
                title: '',
                form: {},
                cityList:[],
                statementsIndex: '',
                formRules: {
                    name: [
                        {required: false, message: '名称不能为空', trigger: 'blur'}
                    ]
                }
                // parentAuthorities: []
            };
        },
        methods: {
            ok () {
                this.$refs.form.validate(valid => {
                    if (valid) {
                        this.loading = true;
                        let request;
                        if (this.form.id) {
                            request = api.userUpdate(this.form);
                        } else {
                            request = api.userInsert(this.form);
                        }
                        request.then(data => {
                            this.loading = false;

                            if (data.code === 0) {
                                this.getData();
                                this.visible = false;
                            }
                        });
                    }
                });
            },
            open (title, id) {
                this.form = {};
                this.visible = true;
                this.title = title;
                this.form.id = id;

                if (id) {
                    api.userDetail({id: id}).then(data => {
                        if (data.code === 0) {
                            this.form = data.data;
                        }
                    });
                }

            },
            cancel () {
                this.visible = false;
            }
        }
    };
</script>
